package com.appspot.finajjarane.bo.mvc.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.finajjarane.framework.generic.ApplicationConstants;
import com.appspot.finajjarane.framework.models.ToolModel;
import com.appspot.finajjarane.framework.models.ToolModelForDao;
import com.appspot.finajjarane.framework.service.IImageService;
import com.appspot.finajjarane.framework.service.IToolService;
import com.google.appengine.api.datastore.Text;


@Controller
@RequestMapping("/tools")
public class Tools {

	private static String URL_NEW = "/bo/tools/new";
	private static String URL_SAVE = "/bo/tools/save";
	private static String URL_SAVE_EDIT = "/bo/tools/save-edit";
	private static String URL_LIST = "/bo/tools/list/1";


	@Autowired
	IToolService iToolService;

	@Autowired
	IImageService iImageService;

	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ModelAndView save(
			@RequestParam("toolTitle")  			String   	 toolTitle,
			@RequestParam("toolTags[]") 			List<String> toolTags,
			@RequestParam("toolDescription_ar_AR")	String   	 toolDescription_ar_AR,
			@RequestParam("toolDescription_en_US")	String   	 toolDescription_en_US,
			@RequestParam("toolDescription_fr_FR")	String   	 toolDescription_fr_FR,
			HttpServletRequest httpServletRequest){



		ToolModelForDao toolModel = new ToolModelForDao();
		Map<String, Text> description = new HashMap<String, Text>();

		description.put("ar_AR", new Text(toolDescription_ar_AR));
		description.put("en_US", new Text(toolDescription_en_US));
		description.put("fr_FR", new Text(toolDescription_fr_FR));

		toolModel.setTitle(toolTitle.trim());
		toolModel.setTags(toolTags);
		toolModel.setDescription(description);
		toolModel.setImage(iImageService.getImageKeyUploaded(httpServletRequest, "toolImage"));

		Map<String, Object> data = new HashMap<String, Object>();

		try {
			iToolService.toolCreateNew(toolModel);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("redirect:" + URL_NEW, data);

	}



	@RequestMapping(value="/new")
	public ModelAndView create(){

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("formUrl", iImageService.createUploadUrl(URL_SAVE));

		return new ModelAndView("tools/new",map);
	}

	@RequestMapping(value="/edit/{id}")
	public ModelAndView edit(@PathVariable int id){
		Map<String, Object> data = new HashMap<String, Object>();
		ToolModel tool;
		try {
			tool = this.iToolService.getTool(new Long(id));
			data.put("tool", tool);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}

		data.put("formUrl", iImageService.createUploadUrl(URL_SAVE_EDIT));

		return new ModelAndView("tools/edit",data);
	}


	@RequestMapping(value="/save-edit", method=RequestMethod.POST)
	public ModelAndView save(
			@RequestParam("toolId")     										Long 		  toolId,
			@RequestParam("toolTitle")  										String   	  toolTitle,
			@RequestParam("toolDescription_ar_AR")								String   	  toolDescription_ar_AR,
			@RequestParam("toolDescription_en_US")								String   	  toolDescription_en_US,
			@RequestParam("toolDescription_fr_FR")								String   	  toolDescription_fr_FR,
			@RequestParam("toolTags[]") 										List<String>  toolTags,
			@RequestParam(value="toolDeleteImage", required = false) 			boolean  	  toolDeleteImage,
			HttpServletRequest httpServletRequest){

		Map<String, Object> data = new HashMap<String, Object>();

		ToolModelForDao toolModelFroDao = new ToolModelForDao();

		try {
			Map<String, Text> description = new HashMap<String, Text>();
			ToolModel toolModel = iToolService.getTool(toolId);

			description.put("ar_AR", new Text(toolDescription_ar_AR));
			description.put("en_US", new Text(toolDescription_en_US));
			description.put("fr_FR", new Text(toolDescription_fr_FR));


			toolModelFroDao.setId(toolId);
			toolModelFroDao.setTitle(toolTitle.trim());
			toolModelFroDao.setDescription(description);
			toolModelFroDao.setTags(toolTags);

			String keyImage = iImageService.getImageKeyUploaded(httpServletRequest, "toolImage");



			if(toolDeleteImage){
				toolModelFroDao.setImage(null);
			}
			else if(null!=keyImage && !keyImage.isEmpty()){
				toolModelFroDao.setImage(keyImage);
			}
			else{
				toolModelFroDao.setImage(this.iToolService.getTool(toolModel.getId()).getImage());
			}

			iToolService.toolUpdate(toolModelFroDao);

		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("redirect:" + URL_LIST, data);
	}

	@RequestMapping(value="/list/{page}")
	public ModelAndView index(@PathVariable("page") int page){
		page=Math.max(page, 1);

		Map<String, Object> data = new HashMap<String, Object>();
		page = Math.max(page, 1);
		List<ToolModel> tools;
		try {
			tools = iToolService.getToolsList(page, ApplicationConstants.NBR_ARTICLES_PER_PAGE_ADMIN, null);
			int toolsCount = iToolService.getToolsCount(null);
			double maxPages = Math.ceil((new Float(toolsCount).floatValue())/ApplicationConstants.NBR_ARTICLES_PER_PAGE_ADMIN);

			data.put("tools", tools);
			data.put("toolsCount", new Integer(toolsCount));
			data.put("maxPages", maxPages);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("tools/list",data);
	}


	@RequestMapping("/remove/{toolId}")
	public ModelAndView remove(@PathVariable Long toolId){

		Map<String, Object> data = new HashMap<String, Object>();

		try {
			iToolService.toolRemove(toolId);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}
		return new ModelAndView("redirect:" + URL_LIST, data);

	}

}
